package com.kb5173.search.itf.impl;

import java.util.List;

import com.kb5173.framework.cache.message.MessageCache;
import com.kb5173.framework.dataaccess.pagination.GenericPage;
import com.kb5173.framework.logging.enums.LogCategory;
import com.kb5173.framework.shared.exception.BusinessException;
import com.kb5173.framework.shared.utils.BeanMapper;
import com.kb5173.search.common.entity.GoodsEO;
import com.kb5173.search.index.service.IIndexService;
import com.kb5173.search.itf.utils.ItfUtil;
import com.kb5173.search.words.service.IWordsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.kb5173.search.itf.ItfSearchService;
import com.kb5173.search.itf.define.enums.ResponseCodesItf;
import com.kb5173.search.itf.dto.AddwordsResponse;
import com.kb5173.search.itf.dto.IndexoptionRequest;
import com.kb5173.search.itf.dto.IndexoptionResponse;
import com.kb5173.search.itf.dto.QueryRequest;
import com.kb5173.search.itf.dto.QueryResponse;


/**
 * 
 * <p>
 * 描述：搜索层开放出的服务接口的实现
 * </p>
 * 
 * @ClassName ItfQueryServiceImpl
 * @author nimin
 * @date 2014-8-6 下午4:32:49
 */
@Component
public class ItfSearchServiceImpl implements ItfSearchService {
    @Autowired
    IIndexService indexService;

    @Autowired
    IWordsService wordsService;

    @Autowired
    private MessageCache messageCache;

	/**
	 *<p>描述：根据游戏区服及关键字，搜索结果</p>
     *
	 *@since 1.0
	 *@author nimin
	 */
	@Override
	public QueryResponse query(QueryRequest queryRequest) {
		QueryResponse queryResponse=new QueryResponse();
		if(null==queryRequest){
			queryResponse.setResponseCodeItf(ResponseCodesItf.IsError);
			queryResponse.setResponseMessage("请求参数为空");
		}
        try {
            GenericPage<GoodsEO> result = indexService.search(queryRequest.getGameName(), queryRequest.getGameArea(),
                    queryRequest.getGameServer(), queryRequest.getType(),
                    queryRequest.getKeyword(), queryRequest.getPageSize(), queryRequest.getPageNub());

            queryResponse.setGenericPage(ItfUtil.transform(result));
        } catch (BusinessException e) {
            queryResponse.setResponseCodeItf(ResponseCodesItf.IsError);
            queryResponse.setResponseMessage(messageCache.getMessage(e.getErrorCode()));
        }
		return queryResponse;
	}

    /**
     *<p>描述：操作solr索引：插入、保存、删除</p>
     *
     *@since 1.0
     *@author nimin
     */
	@Override
	public IndexoptionResponse indexoption(IndexoptionRequest indexoptionRequest) {
		IndexoptionResponse indexoptionResponse=new IndexoptionResponse();
		if(null==indexoptionRequest){
			indexoptionResponse.setResponseCodeItf(ResponseCodesItf.IsError);
			indexoptionResponse.setResponseMessage("请求参数为空");
		}
        try {
            GoodsEO goodsEO = BeanMapper.map(indexoptionRequest, GoodsEO.class);
            indexService.optionDocument(indexoptionRequest.getOptionType(), goodsEO);
            indexoptionResponse.setResponseCodeItf(ResponseCodesItf.IsSuccess);
        } catch (BusinessException e) {
            indexoptionResponse.setResponseCodeItf(ResponseCodesItf.IsError);
            indexoptionResponse.setResponseMessage(messageCache.getMessage(e.getErrorCode()));
        }
		return indexoptionResponse;
	}

    /**
     *<p>描述：自定义ik分词</p>
     *
     *@since 1.0
     *@author nimin
     */
	@Override
	public AddwordsResponse addwords(List<String> words) {
		AddwordsResponse addwordsResponse=new AddwordsResponse();
		if(null==words){
			addwordsResponse.setResponseCodeItf(ResponseCodesItf.IsError);
			addwordsResponse.setResponseMessage("请求参数为空");
		}
        try {
            wordsService.addWords(words);
            addwordsResponse.setResponseCodeItf(ResponseCodesItf.IsSuccess);
        } catch (BusinessException e) {
            addwordsResponse.setResponseCodeItf(ResponseCodesItf.IsError);
            addwordsResponse.setResponseMessage(messageCache.getMessage(e.getErrorCode()));
        }
		return addwordsResponse;
	}

}
